cssparser: Improve location APIs
authorBenjamin Otte <otte@redhat.com>
Wed, 10 Apr 2019 14:02:07 +0000 (16:02 +0200)
committerBenjamin Otte <otte@redhat.com>
Fri, 12 Apr 2019 17:34:28 +0000 (19:34 +0200)
1. Export multiple locations
2. Return the location instead of passing one in

gtk/css/gtkcssparser.c
gtk/css/gtkcssparserprivate.h
gtk/gtkcsssection.c

index 6f6339393183149f5c81673996c3761951457bc4..1ddad5712f8ef4453ca76cd582136009ade1ec78 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "gtkcssenums.h"
 #include "gtkcsserror.h"
+#include "gtkcsslocationprivate.h"
 
 typedef struct _GtkCssParserBlock GtkCssParserBlock;
 
@@ -197,17 +198,79 @@ gtk_css_parser_resolve_url (GtkCssParser *self,
 }
 
 /**
- * gtk_css_parser_get_location:
+ * gtk_css_parser_get_start_location:
+ * @self: a #GtkCssParser
+ *
+ * Queries the location of the current token.
+ *
+ * This function will return the location of the start of the
+ * current token. In the case a token has been consumed, but no
+ * new token has been queried yet via gtk_css_parser_peek_token()
+ * or gtk_css_parser_get_token(), the previous token's start
+ * location will be returned.
+ *
+ * This function may return the same location as
+ * gtk_css_parser_get_end_location() - in particular at the
+ * beginning and end of the document.
+ *
+ * Returns: the start location
+ **/
+const GtkCssLocation *
+gtk_css_parser_get_start_location (GtkCssParser *self)
+{
+  return &self->location;
+}
+
+/**
+ * gtk_css_parser_get_end_location:
  * @self: a #GtkCssParser
  * @out_location: (caller-allocates) Place to store the location
  *
- * Queries the current location of the parser.
+ * Queries the location of the current token.
+ *
+ * This function will return the location of the end of the
+ * current token. In the case a token has been consumed, but no
+ * new token has been queried yet via gtk_css_parser_peek_token()
+ * or gtk_css_parser_get_token(), the previous token's end location
+ * will be returned.
+ *
+ * This function may return the same location as
+ * gtk_css_parser_get_start_location() - in particular at the
+ * beginning and end of the document.
+ *
+ * Returns: the end location
  **/
-void
-gtk_css_parser_get_location (GtkCssParser   *self,
-                             GtkCssLocation *out_location)
+const GtkCssLocation *
+gtk_css_parser_get_end_location (GtkCssParser *self)
 {
-  *out_location = self->location;
+  return gtk_css_tokenizer_get_location (self->tokenizer);
+}
+
+/**
+ * gtk_css_parser_get_block_location:
+ * @self: a #GtkCssParser
+ *
+ * Queries the start location of the token that started the current
+ * block that is being parsed.
+ *
+ * If no block is currently parsed, the beginning of the document
+ * is returned.
+ *
+ * Returns: The start location of the current block
+ */
+const GtkCssLocation *
+gtk_css_parser_get_block_location (GtkCssParser *self)
+{
+  GtkCssParserBlock *block;
+
+  if (self->blocks->len == 0)
+    {
+      static const GtkCssLocation start_of_document = { 0, };
+      return &start_of_document;
+    }
+  
+  block = &g_array_index (self->blocks, GtkCssParserBlock, self->blocks->len - 1);
+  return &block->start_location;
 }
 
 static void
index f39d0025a36f558913ad86378d18ad13049a3403..fa6b5757fb047ae62d20decbbd7daee183858c74 100644 (file)
@@ -65,8 +65,10 @@ void                    gtk_css_parser_unref                    (GtkCssParser
 GFile *                 gtk_css_parser_get_file                 (GtkCssParser                   *self);
 GFile *                 gtk_css_parser_resolve_url              (GtkCssParser                   *self,
                                                                  const char                     *url);
-void                    gtk_css_parser_get_location             (GtkCssParser                   *self,
-                                                                 GtkCssLocation                 *out_location);
+
+const GtkCssLocation *  gtk_css_parser_get_start_location       (GtkCssParser                   *self);
+const GtkCssLocation *  gtk_css_parser_get_end_location         (GtkCssParser                   *self);
+const GtkCssLocation *  gtk_css_parser_get_block_location       (GtkCssParser                   *self);
 
 const GtkCssToken *     gtk_css_parser_peek_token               (GtkCssParser                   *self);
 const GtkCssToken *     gtk_css_parser_get_token                (GtkCssParser                   *self);
index 6fae99d6bfbbebfc8ac2f36c6ee4d71eb98fdf77..c581ca952f9be6d9a6a67e44196d03776f40ae19 100644 (file)
@@ -51,7 +51,7 @@ gtk_css_section_new_for_parser (GtkCssSection     *parent,
   if (section->file)
     g_object_ref (section->file);
   section->parser = parser;
-  gtk_css_parser_get_location (section->parser, &section->start_location);
+  section->start_location = *gtk_css_parser_get_start_location (section->parser);
 
   return section;
 }
@@ -62,7 +62,7 @@ _gtk_css_section_end (GtkCssSection *section)
   gtk_internal_return_if_fail (section != NULL);
   gtk_internal_return_if_fail (section->parser != NULL);
 
-  gtk_css_parser_get_location (section->parser, &section->end_location);
+  section->end_location = *gtk_css_parser_get_end_location (section->parser);
   section->parser = NULL;
 }
 
@@ -181,7 +181,7 @@ gtk_css_section_get_end_location (const GtkCssSection *section)
   gtk_internal_return_val_if_fail (section != NULL, NULL);
 
   if (section->parser)
-    gtk_css_parser_get_location (section->parser, (GtkCssLocation *) &section->end_location);
+    return gtk_css_parser_get_end_location (section->parser);
 
   return &section->end_location;
 }